package cn.bigcontainer.alt.meta.extension.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.bigcontainer.alt.common.util.WoVelocityKit;
import cn.bigcontainer.alt.meta.extension.MButtonFilter;
import cn.bigcontainer.alt.meta.po.MButton;

@Component(MButtonFilter.DEFAULT)
public class VelocityButtonFilter implements MButtonFilter {

	@Resource
	private WoVelocityKit velocityKit;
	
	@Override
	public Boolean isVisible(MButton button, Map<String, Object> row) {
		String beanString = button.getButtonFilterBeanId();
		Map<String, Object> dataMap = new HashMap<>();
		for (String key : row.keySet()) {
			int index = key.indexOf('_');
			dataMap.put(key.substring(index + 1), row.get(key));
		}
		dataMap.putAll(row);
		String result = velocityKit.parse(String.format("#if(%s) true #else false #end", beanString), dataMap);
		return "true".equals(result);
	}

}
